var util = require('../../utils/utilq.js');
const userApi = require('../../config').userApi;
Page({
    data: {
        email: '931307458@qq.com',
        verifyCode: '',
        leftTime: -1,
        isSendingCode: false
    },
    // 获得邮箱
    onEmailInput: function (e) {
        this.setData({
            email: e.detail.value
        });
    },
    // 获得验证码
    onVerifyInput: function (e) {
        this.setData({
            verifyCode: e.detail.value
        });
    },
    // 发送验证码
    onSendVerifyCode: function () {
        let that = this;
        if (this.data.isSendingCode) return; //正在请求数据中
        // 验证手机号码
        let email = this.data.email;
        if (!util.checkEmail(email)) {
            console.log(email)
            util.showToast('邮箱有误')
            return;
        }
        wx.showLoading({
            title: '加载中',
            mask: true
        });
        // 设置数据
        this.setData({
            isSendingCode: true
        });
        util.request({
            url: userApi + '/email',
            data: {
                email: email
            },
            success: function (res) {
                let data = res.data;
                console.log(res)
                // 解密;
                if (0 === data.code) {
                    util.showToast(data.msg)
                    // 设置倒计时
                    let leftTime = 60;
                    that.setData({
                        leftTime: leftTime
                    });

                    let si = setInterval(function countdown() {
                        leftTime--;
                        that.setData({
                            leftTime: leftTime
                        });

                        // 倒计时结束
                        if (leftTime < 0) {
                            clearInterval(si);
                        }
                    }, 1000);
                } else {
                    util.showToast(data.msg || '网络异常，请稍后再试');
                }
            },
            fail: function (err) {
                util.showToast('网络异常，请稍后再试');
            },
            complete: function () {
                wx.hideLoading();
                that.setData({
                    isSendingCode: false
                });
            }
        });
    },

    // 下一步
    logging: function () {
        let email = this.data.email;
        let verifyCode = this.data.verifyCode;

        // 验证手机号码
        if ('' === email) {
            util.showToast('请输入邮箱');
            return;
        } else if (!util.checkEmail(email)) {
            util.showToast('邮箱格式有误');
            return;
        }

        //验证验证码 
        if ('' === verifyCode) {
            util.showToast('请输入验证码');
            return;
        } else if (verifyCode.length != 4) {
            util.showToast('请输入正确的验证码');
            return;
        }
        util.request({
            url: userApi +'/login',
            data: {
                email:email,
                code: verifyCode
            },
            success: function (res) {
                let data = res.data;
                // 解密;
                if (0 === data.verifyCode) {

                } else {
                    util.showToast(data.msg || '网络异常，请稍后再试');
                }
            },
            fail: function (err) {
                util.showToast('网络异常，请稍后再试');
            },
            complete: function () {
                wx.hideLoading();
                that.setData({
                    isSendingCode: false
                });
            }
        });
    },
    onShow: function () {
    }
})